#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time   : 2022-06-06 17:51:04
# @Author : 搬砖客


import requests
from utils.process_files_utils.yaml_util import YamlUtil
from utils.other_tools.allureData.allure_report_data import CaseCount
from utils.reports_utils.get_jenkins_job import get_allure_url

report_switch = YamlUtil.read_config_yaml("report", "enable")
report_type = YamlUtil.read_config_yaml("report", "type")

case = CaseCount()
jenkins_url = str(YamlUtil.read_config_yaml("host", "jenkins_host")) + ":" + str(
    YamlUtil.read_config_yaml("port", "jenkins_port"))
job_name = str(YamlUtil.read_config_yaml("project", "name"))
if report_switch:
    if report_type == "local":
        allure_url = "报告请点击report文件中的'index.html' 查看"
    else:
        allure_url = get_allure_url()
else:
    allure_url = "没有开启报告"


class SendReport:
    # 将测试结果发送到钉钉消息群，webhook通过config文件配置
    @classmethod
    def send_ding(cls):
        cont = f"自动化测试已经完成，结果为：\n" \
               f"测试结果通过的数量为：{case.pass_count()}\n" \
               f"测试结果失败的数量为：{case.failed_count()}\n" \
               f"测试结果异常的数量为：{case.broken_count()}\n" \
               f"测试结果跳过的数量为：{case.skipped_count()}\n" \
               f"测试结用例总的数量为：{case.total_count()}\n" \
               f"测试成功率为：{case.pass_rate()}%\n" \
               f"测试时长为：{case.get_times()} 秒\n" \
               f"测试报告地址为：{allure_url}\n"
        data = {
            "msgtype": "text",
            "text": {
                "content": cont
            },
            "at": {
                # 要@的人，手机号
                "atMobiles": [],
                # 是否@所有人
                "isAtAll": False
            }
        }
        url = YamlUtil.read_config_yaml("url", "ding_Webhook")
        headers = {'Content-Type': 'application/json'}
        requests.post(url=url, headers=headers, json=data)
